/**
 * Created by Administrator on 2016/10/21.
 */
class CardView extends egret.DisplayObjectContainer {

    static create(dir: number, style: number, pai: any = null, count = 1) {
        let card = CardView.getCardView();
        card.dir = dir;
        card.style = style;
        card.pai = pai;
        card.count = count;
        card.reDraw();
        return card;
    }

    static copy(item: CardView) {
        let card = CardView.getCardView();
        card.alpha = item.alpha;
        card.scaleX = item.scaleX;
        card.scaleY = item.scaleY;
        card.x = item.x;
        card.y = item.y;
        card.dir = item.dir;
        card.style = item.style;
        card.pai = item.pai;
        card.count = item.count;
        card.reDraw();
        return card;
    }

    static createThreeGroup(dir: number, style: number, pais: any[] = null) {
        if (pais == null) pais = [];

        let group = [];
        for (let i: number = 0; i < 3; i++) {
            let o = GSConfig.getPosByIndex(dir, style, i);
            let cardView = CardView.create(dir, style, pais[i]);
            cardView.posView(o.x, o.y);
            group.push(cardView);
        }
        return group;
    }

    static getCardView() {
        if (CardView.pool.length) return CardView.pool.shift();

        let cardView = new CardView();
        cardView.reset();
        return cardView;
    }

    static returnCardView(card: CardView) {
        card.reset();
        CardView.pool.push(card);
    }

    static text_big_arg = { y: 70, size: 35 };
    static text_small_arg = { y: 30, size: 18 };

    static pool: CardView[] = [];

    dir: number;
    style: number;
    pai: any;

    pos: egret.Point;

    bgCon: egret.DisplayObjectContainer;
    bg: egret.Bitmap;
    light: egret.Bitmap;

    top: egret.DisplayObjectContainer;
    icon: egret.Bitmap;
    countText: egret.TextField;
    d: egret.Shape;
    pRule: Rule;
    index: number;
    count: number;
    hotArea: eui.Rect;

    funcPos: eui.Image;
    //iconBG:eui.Rect;
    constructor() {
        super();

        this.bgCon = new egret.DisplayObjectContainer;
        this.addChild(this.bgCon);

        this.bg = new egret.Bitmap;
        this.bgCon.addChild(this.bg);

        this.light = new egret.Bitmap;
        this.bgCon.addChild(this.light);

        this.top = new egret.DisplayObjectContainer();
        this.addChild(this.top);

        this.icon = new egret.Bitmap;
        this.top.addChild(this.icon);

        this.countText = new egret.TextField;
        this.countText.bold = true;
        this.countText.background = true;
        this.countText.backgroundColor = 0;
        this.countText.textColor = 0x8ebd49;

        //this.countText.x = 24;
        //this.countText.y = 70;

        this.addChild(this.top);

        this.pos = new egret.Point;

        //碰杠指向
        this.funcPos = new eui.Image;
        this.funcPos.visible = true;
        this.top.addChild(this.funcPos);
        this.funcPos.touchEnabled = false;

        //
        /*        this.hotArea = new egret.Shape;
         this.hotArea.graphics.beginFill(0, 0);
         this.hotArea.graphics.drawRect(0, 0, 64, 120);
         this.addChild(this.hotArea);
         this.hotArea.anchorOffsetX = GSConfig.posRule[1][1].bgosX;
         this.hotArea.anchorOffsetY = GSConfig.posRule[1][1].bgosY;*/

        this.top.addChild(this.countText);


        this.hotArea = new eui.Rect();
        this.hotArea.width = 64;
        this.hotArea.height = 120;
        this.hotArea.alpha = 0;
        this.hotArea.touchEnabled = false;
        this.hotArea.anchorOffsetX = GSConfig.posRulePlus[1][1].bgosX;
        this.hotArea.anchorOffsetY = GSConfig.posRulePlus[1][1].bgosY;
        this.addChild(this.hotArea);

        this.pos = new egret.Point;

        this.unactivate();
        this.updateColor();

        gameManager.i.addEventListener(GameEventType.UPDATE_CARDCOLOR, this.updateColor, this);
    }

    private updateColor() {
        let resource: string;

        switch (game.paiColor) {
            case 0:
                resource = 'card_blue_';
                break;
            case 1:
                resource = 'card_green_';
                break;
            case 2:
                resource = 'card_yellow_';
                break;
        }

        this.bg.texture = RES.getRes(resource + this.resType);
        this.light.texture = this.lightRes ? RES.getRes(this.lightRes) : null;
    }

    activate() {
        this.touchEnabled = this.hotArea.visible = true;
    }

    unactivate() {
        this.touchEnabled = this.hotArea.visible = false;
        this.enabled = true;
        this.lighted = false;
    }

    set enabled(value) {
        this.bg.alpha = value ? 1 : .5;
    }

    set lighted(value) {
        this.light.visible = value;
    }

    //移出
    moveUp(tween: boolean = true) {
        egret.Tween.removeTweens(this);


        if (tween) egret.Tween.get(this).to({ y: this.pos.y - GSConfig.moveUpDis }, GSConfig.cardMoveTime);

        else this.y = this.pos.y - GSConfig.moveUpDis;
    }

    //移回
    moveDown(tween: boolean = true) {
        egret.Tween.removeTweens(this);


        if (tween) egret.Tween.get(this).to({ y: this.pos.y }, GSConfig.cardMoveTime);

        else this.y = this.pos.y;
    }

    //重置位置
    resetPos() {
        this.x = this.pos.x;
        this.y = this.pos.y;
    }

    //添加点击
    addClick(func: Function, thisObj: any) {
        if (!this.hasEventListener(egret.TouchEvent.TOUCH_TAP)) {
            this.addEventListener(egret.TouchEvent.TOUCH_TAP, func, thisObj);
        }
    }

    //设置坐标
    posView(x: number, y: number) {
        this.pos.x = x;
        this.pos.y = y;

        this.x = x;
        this.y = y;
    }




    changeScale(scale: number) {
        this.scaleX = this.scaleY = scale;
    }

    //改变样式
    changeStyle(style: number, draw: boolean = true) {
        this.style = style;
        this.count = 1;
        draw && this.reDraw();
    }

    //改变数据
    changePai(pai: any, draw: boolean = true) {
        this.pai = pai;
        this.count = 1;
        draw && this.reDraw();
    }

    showD() {
        this.d = new egret.Shape;
        this.d.graphics.beginFill(0xff0000);
        this.d.graphics.drawRect(0, 0, 10, 10);
        this.addChild(this.d);
    }

    //重置
    reset() {
        this.posView(0, 0);
        this.count = 1;
        this.index = -1;
        this.scaleX = this.scaleY = 1;
        this.visible = true;
        this.unactivate();
        egret.Tween.removeTweens(this);
    }

    /**碰杠 方向指向箭头 配置 */
    private funcsPosConfig = {
        1: [26, 60, 29],
        2: [14, 30, 19],
        3: [15, 25, 19],
        4: [14, 30, 19],

    };
    /** */
    private angleTingPosConfig = {
        1: [27, -32, 29]
    };
    /**结构 {牌所在方向:{来源方向:rotation}} */
    private funcsAngleConfig = {
        1: { 1: 180, 2: 90, 3: 0, 4: -90 },
        2: { 1: -90, 2: 0, 3: 90, 4: 0 },
        3: { 1: 180, 2: 90, 3: 0, 4: -90 },
        4: { 1: 90, 2: 0, 3: -90, 4: 180 }
    };
    public angleType: AngleType;
    /**设置碰杠 方向指向箭头 方向 位置 大小 */
    public setFuncsIcon(dir: number, angleType?: AngleType) {

        this.funcPos.visible = !!dir;

        if (!dir) {
            return
        };

        this.angleType = angleType;



        let config;
        let rotation: number;
        switch (angleType) {
            case AngleType.tingPrompt:
                config = this.angleTingPosConfig;
                rotation = 0;
                this.funcPos.source = "icon_ting";
                break;
            default:
                config = this.funcsPosConfig;
                rotation = this.funcsAngleConfig[this.dir][dir];
                this.funcPos.source = this.dir == 1 ? "icon_pos_l" : "icon_pos_s";
                break;
        }

        const [x, y, width] = config[this.dir] as [number, number, number];

        this.funcPos.anchorOffsetX = width / 2;
        this.funcPos.anchorOffsetY = width / 2;

        this.funcPos.rotation = rotation;

        this.funcPos.x = x;
        this.funcPos.y = y;

    }

    resType: number;

    lightRes: string;


    reDraw() {

        this.pRule = GSConfig.posRulePlus[this.dir][this.style];

        let type = this.dir * 10 + this.style;

        //字的尺寸类型
        let ZType = "X_";

        var text_arg = CardView.text_small_arg;
        /**提示箭头 */
        this.funcPos.visible = false;
        this.angleType = undefined;


        switch (type) {
            case 11:
            case 12:
            case 13:
                ZType = "Z_";
                text_arg = CardView.text_big_arg;
                break;
            case 14:
                type = 33;
                break;
            case 15:
                type = 12;
                break;
            case 24:
                type = 23;
                break;
            case 34:
                type = 33;
                break;
            case 41:
                type = 21;
                break;
            case 42:
                type = 22;
                break;
            case 43:
                type = 23;
                break;
            case 44:
                type = 23;
                break;
        }
        this.resType = type;

        this.lightRes = this.pRule.light;

        this.updateColor();

        this.countText.y = text_arg.y;
        this.countText.size = text_arg.size;


        if (this.count > 1) {
            this.countText.text = "x" + this.count;
        } else {
            this.countText.text = "";
        }

        if (this.style == 2 || this.style == 5) {
            this.icon.texture = null;
            this.countText.text = "";
        } else {
            if (this.pai != null) {
                let up_res = ZType + (this.pai.type * 10 + this.pai.number);
                this.icon.texture = RES.getRes(up_res);

            } else {
                this.countText.text = "";
                this.icon.texture = null;
            }
        }

        this.bgCon.anchorOffsetX = this.pRule.bgosX;
        this.bgCon.anchorOffsetY = this.pRule.bgosY;

        this.bgCon.scaleX = this.pRule.bgScaleX;
        this.bgCon.scaleY = this.pRule.bgScaleY;

        this.top.anchorOffsetX = this.pRule.toposX;
        this.top.anchorOffsetY = this.pRule.toposY;

        this.top.rotation = this.pRule.topRot;

        this.top.scaleX = this.pRule.topScaleX;
        this.top.scaleY = this.pRule.topScaleY;
        this.changeScale(this.pRule.scale);
    }
}

